package com.jd.dl.service;

import com.jd.dl.dao.AdminDAO;
import com.jd.dl.dao.TaskDAO;
import com.jd.dl.dao.constant.QueryConstants;
import com.jd.dl.dao.util.HqlCriterions;
import com.jd.dl.pojo.Admin;
import com.jd.dl.pojo.User;
import com.jd.dl.service.exception.ErrorCode;
import com.jd.dl.service.exception.ErrorMessage;
import com.jd.dl.service.exception.ServiceException;

public class AdminService {

	private AdminDAO adminDAO;
	private TaskDAO taskDAO;


	public User load(long id) {
//		User user = (User) userDAO.loadById(id);
//		List<Permission> per = user.getRole().getPermissions();
//		for (Permission permission : per) {
//			userDAO.initialize(permission);
//		}
//		return user;
		return null;
	}

	

//	public void add(final User user) throws ServiceException {
//		HqlCriterions hc = new HqlCriterions();
//		hc.and(QueryConstants.User.USER_ID, user.getPhoneNumber(),
//				QueryConstants.EQ);
//
//		if (userDAO.getCountByQuery(hc.getHqlString()) > 0) {
//			String errMsg = CommonUtils.formatString(ErrorMessage.DUPLICATION_ENTITY_USER, 
//					new String[]{user.getPhoneNumber()});
//			throw new ServiceException(ErrorCode.DUPLICATION_ENTITY, errMsg);
//		}
//
//		User newUser = new User();
//		newUser.setPhoneNumber(user.getPhoneNumber());
//
//
//		newUser.setCreatedDate(new Date());
//		newUser.setLastModified(newUser.getCreatedDate());
//
//		userDAO.saveOrUpdate(newUser);
//
//	}

//	public void update(final User user) throws ServiceException {
//		HqlCriterions hc = new HqlCriterions();
//		hc.and(QueryConstants.User.PHONE_NUMBER, user.getPhoneNumber(),
//				QueryConstants.EQ);
//		hc.and(QueryConstants.User.USER_ID, user.getUserId(), QueryConstants.NE);
//
//		if (userDAO.getCountByQuery(hc.getHqlString()) > 0) {
//			String errMsg = CommonUtils.formatString(ErrorMessage.DUPLICATION_ENTITY, 
//					new String[]{user.getPhoneNumber()});
//			throw new ServiceException(ErrorCode.DUPLICATION_ENTITY, errMsg);
//		}
//
//		User newUser = (User) userDAO.loadById(user.getUserId());
//
//
//
//		newUser.setLastModified(new Date());
//	
//
//	}



	public boolean hasPermission(long id, long functionId) {
//		User user = (User) userDAO.getById(id);
//		for (Permission permission : user.getRole().getPermissions()) {
//			if (functionId == permission.getFunction().getFunctionId()) {
//				return true;
//			}
//		}
		return false;
	}

	public Admin login(String adminName, String adminPWD) throws ServiceException {
		
		HqlCriterions hc = new HqlCriterions();
		hc.and(QueryConstants.Admin.ADMIN_NAME, adminName, QueryConstants.EQ);
		//hc.and(QueryConstants.User.deleted, false, QueryConstants.EQ);
		Admin admin = (Admin) adminDAO.getOneByQuery(hc.getHqlString(), true);

		
		if (admin == null) {
			throw new ServiceException(ErrorCode.USER_NOT_EXIST, ErrorMessage.INCORRECT_PASSWORD);
		}
		if (!admin.getAdminPwd().equals(adminPWD)) {
			throw new ServiceException(ErrorCode.PWD_NOT_MATCH, ErrorMessage.INCORRECT_PASSWORD);
		}
//		userDAO.initialize(user.getRole());
//		functionDAO.initialize(user.getRole().getFunctions());
//		permissionDAO.initialize(user.getRole().getPermissions());
		return admin;
	}

//	public void setUserDAO(UserDAO userDAO) {
//		this.userDAO = userDAO;
//	}

	public TaskDAO getTaskDAO() {
		return taskDAO;
	}

	public void setTaskDAO(TaskDAO taskDAO) {
		this.taskDAO = taskDAO;
	}



	public AdminDAO getAdminDAO() {
		return adminDAO;
	}



	public void setAdminDAO(AdminDAO adminDAO) {
		this.adminDAO = adminDAO;
	}



}
